package org.gruposp2p.aula.gwt.client.content.student;

import java.util.ArrayList;
import java.util.List;
import org.gruposp2p.aula.gwt.client.AulaApplication;
import org.gruposp2p.aula.gwt.client.AulaConstants;
import org.gruposp2p.aula.gwt.client.AulaContentWidget;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class StudentPanelWidget extends AulaContentWidget {
	
	public static interface CwConstants extends Constants {
		String StudentPanelWidgetStudentLabel ();
		String StudentPanelWidgetAbsencesLabel ();
		String StudentPanelWidgetScoreLabel ();
		String StudentPanelWidgetTabTitle ();
	}
	
	private AulaApplication aulaApplication;
	private AulaConstants constants;
	private StudentDataPanel dataPanel;
	private StudentScoresPanel scoresPanel;
	private StudentAbsencesPanel absencesPanel;
	private ToggleButton dataToggleButton;
	private ToggleButton absencesToggleButton;
	private ToggleButton scoresToggleButton;
	private List<ToggleButton> toggleButtons;
	private Panel selectePanel;
	private HorizontalPanel containerPanel;
	private VerticalPanel tabbedPanel;
	
	public StudentPanelWidget (AulaApplication aulaApplication) {
		this.aulaApplication = aulaApplication;
		this.constants = aulaApplication.getConstants();
		toggleButtons = new ArrayList<ToggleButton>();
		dataToggleButton = new ToggleButton(constants.StudentPanelWidgetStudentLabel());
		dataToggleButton.addClickHandler(dataButtonHandler);
		dataToggleButton.setStylePrimaryName("aula-ToggleButton");
		toggleButtons.add(dataToggleButton);
		absencesToggleButton = new ToggleButton(constants.StudentPanelWidgetAbsencesLabel());
		absencesToggleButton.addClickHandler(absencesButtonHandler);
		absencesToggleButton.setStylePrimaryName("aula-ToggleButton");
		toggleButtons.add(absencesToggleButton);
		scoresToggleButton = new ToggleButton(constants.StudentPanelWidgetScoreLabel());
		scoresToggleButton.addClickHandler(scoresButtonHandler);
		scoresToggleButton.setStylePrimaryName("aula-ToggleButton");
		toggleButtons.add(scoresToggleButton);		
		dataPanel = new StudentDataPanel (aulaApplication);
		scoresPanel = new  StudentScoresPanel (aulaApplication);
		absencesPanel = new  StudentAbsencesPanel (aulaApplication);
	    containerPanel = new HorizontalPanel();
	    tabbedPanel = new VerticalPanel();
	}
	
	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getName() {
		return constants.StudentPanelWidgetTabTitle();
	}

	@Override
	public Widget onInitialize() {
	    final ListBox studentsListBox = new ListBox(true);
	    studentsListBox.ensureDebugId("cwListBox-multiBox");
	    studentsListBox.setWidth("13em");
	    studentsListBox.setVisibleItemCount(15);
	    VerticalPanel studentsListBoxPanel = new VerticalPanel();
	    studentsListBoxPanel.setSpacing(4);
	    //multiBoxPanel.add(new HTML(constants.cwListBoxSelectAll()));
	    studentsListBoxPanel.add(studentsListBox);
	    containerPanel.add(studentsListBoxPanel);
	    // Add a handler to handle drop box events
	    studentsListBox.addChangeHandler(new ChangeHandler() {
	      public void onChange(ChangeEvent event) {
	    	  showStudent(studentsListBox);
	      }
	    });

		HorizontalPanel toggleButtonPanel = new HorizontalPanel();
		toggleButtonPanel.setStylePrimaryName("AulaWidgetInternalTab");
		toggleButtonPanel.add(dataToggleButton);
		toggleButtonPanel.add(absencesToggleButton);
		toggleButtonPanel.add(scoresToggleButton);
		tabbedPanel.add(toggleButtonPanel);
		containerPanel.add(tabbedPanel);
		
		dataToggleButton.setDown(true);
		selectePanel = dataPanel;
		tabbedPanel.add(selectePanel);
	    return containerPanel;
	}
	
	private void setSelectedPanel(Panel panel) {	
		tabbedPanel.remove(selectePanel);
		this.selectePanel = panel;
		tabbedPanel.add(panel);		
	}
	
	private void showStudent (ListBox multiBox) { }

	private void setButtonsState (ClickEvent event) {
		ToggleButton source = (ToggleButton) event.getSource();
		for (ToggleButton toggleButton : toggleButtons) {
			if (!toggleButton.equals(source)) {
				toggleButton.setDown(false);
			} else {
				toggleButton.setDown(true);
			}
		}
	}
	
	ClickHandler dataButtonHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {	
			setButtonsState (event);
			setSelectedPanel(dataPanel);
		}		
	};	
	
	ClickHandler absencesButtonHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {	
			setButtonsState (event);	
			setSelectedPanel(absencesPanel);
		}		
	};	
	
	ClickHandler scoresButtonHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent event) {	
			setButtonsState (event);		
			setSelectedPanel(scoresPanel);
		}		
	};
	
}